package com.hq.ims.data.aop;

import com.alibaba.fastjson2.JSONObject;
import com.hq.ims.data.annotation.DataRangeConcatenation;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entityenum.EnumCompanyLevel;
import com.hq.ims.data.interiorenum.EnumInsideFindDataScore;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.utils.CurThreadLocalUtil;
import com.hq.ims.data.utils.JsonAnalyzeUtil;
import com.hq.ims.data.utils.WebExpandUtils;
import com.hq.utils.util.EnumUtils;
import com.hq.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Resource;

/**
 * 用户操作aop
 */
@Aspect
@Component
@Slf4j
public class DataRangeConcatenationAop {

    private final String ATTR_REQUEST_JSON_OBJECT = "requestJsonObject";

    @Resource
    private BaseCompanyService baseCompanyService;

    // 以自定义 @WebLog 注解为切点
    @Pointcut("@annotation(com.hq.ims.data.annotation.DataRangeConcatenation)")
    public void DataRangeConcatenationAop() {
    }

    @Before(value = "DataRangeConcatenationAop()")
    public void before(JoinPoint joinPoint) {
        DataRangeConcatenation annotation = (DataRangeConcatenation) ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(DataRangeConcatenation.class);
        JSONObject jsonObject = (JSONObject) RequestContextHolder.getRequestAttributes().getAttribute(ATTR_REQUEST_JSON_OBJECT, RequestAttributes.SCOPE_REQUEST);
        //查询组织ID
        String orgIdKey = annotation.orgIdKey();
        String orgIdValue = JsonAnalyzeUtil.findJsonValue(jsonObject, orgIdKey);

        //查询组织机构等级
        String orgLvKey = annotation.orgLvKey();
        String orgLvValue = JsonAnalyzeUtil.findJsonValue(jsonObject, orgLvKey);
        EnumCompanyLevel orgLv = null;
        if (StringUtils.isNotEmpty(orgLvValue)) {
            orgLv = EnumUtils.getByName(EnumCompanyLevel.class, orgLvValue);
        }
        orgLv = orgLv == null ? EnumCompanyLevel.GeoLogical : orgLv;

        //如果没有给默认值
        if (StringUtils.isEmpty(orgIdValue)) {
            orgIdValue = WebExpandUtils.getHeadCompanyId();
            Company company = baseCompanyService.getById(orgIdValue);
            orgLv = company.getCompanyLevel();
        }

        //查询权限范围查询
        String findDataScoreKey = annotation.findDataScoreKey();
        String findDataScoreKeyValue = JsonAnalyzeUtil.findJsonValue(jsonObject, findDataScoreKey);
        EnumInsideFindDataScore findDataScore = null;
        if (StringUtils.isNotEmpty(findDataScoreKeyValue)) {
            findDataScore = EnumUtils.getByName(EnumInsideFindDataScore.class, findDataScoreKeyValue);
        }
        findDataScore = findDataScore == null ? EnumInsideFindDataScore.CurrentAndBelow : findDataScore;

        //设置存储
        CurThreadLocalUtil.ThreadStore threadStore = CurThreadLocalUtil.getInstance().get();
        threadStore.setOrgId(orgIdValue);
        threadStore.setOrgLv(orgLv);
        threadStore.setFindDataScore(findDataScore);
        threadStore.setDataLibCompanyFieldName(annotation.dataLibCompanyFieldName());
        threadStore.setDataLibProjectFieldName(annotation.dataLibProjectFieldName());
    }

    @After(value = "DataRangeConcatenationAop()")
    public void after(JoinPoint joinPoint) {
    }
}
